#include "widget.h"
#include "ui_widget.h"
#include "agressform.h"
#include "addform.h"
#include <QDebug>
#include <QDateTime>
#include <QDialog>
#include <QFile>
#include <QDir>
#include <QLineEdit>
#include <QTimer>
#include <QMenu>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setHidden(false);
    this->setWindowTitle("一个小目标");

    t5=new QTimer(this);
    t6=new QTimer(this);
    AgressForm *agree1=new AgressForm();
    //agree1->show();
    agree1->resize(500,500);




    QTimer *t=new QTimer(this);
    //定时器具体任务
    connect(t,&QTimer::timeout,[this](){
        ui->lcdNumber->display(++this->n1);
    });
    connect(ui->pushButton,&QPushButton::clicked,[t](){
        //启动定时器
        t->start(1000);
    });
    connect(ui->pushButton_2,&QPushButton::clicked,[t](){
        //停止定时器
        t->stop();
    });
    connect(ui->pushButton_4,&QPushButton::clicked,[t,this](){
        //清空定时器
        t->stop();
        this->n1=0;
        ui->lcdNumber->display(this->n1);
    });





    //时钟
    ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
    QTimer *timeLock=new QTimer(this);
    timeLock->start(1000);
    connect(timeLock,&QTimer::timeout,[this](){
        ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
    });





    this->reflush();
    connect(ui->pushButton_3,&QPushButton::clicked,[this](){
        this->planText="";
        this->planTime="";
        t5->stop();
        this->reflush();
    });



    this->setContextMenuPolicy(Qt::CustomContextMenu);
    this->winMenu=new QMenu;
    QAction *add=this->winMenu->addAction("新增");
    QAction *reflu=this->winMenu->addAction("刷新");
    connect(add,&QAction::triggered,[this](){
        this->on_pushButton_5_clicked();
    });

    connect(reflu,&QAction::triggered,[this](){
        this->planText="";
        this->planTime="";
        t5->stop();
        this->reflush();
    });




}

Widget::~Widget()
{
    delete ui;
}


void Widget::reflush()
{

    ui->label->setText(this->planText);

    //倒计时
    //获取结束时间戳
    QString currentPath = QDir::currentPath();
    QFile file(currentPath+"/plan.txt");
    if (file.open(QIODevice::ReadOnly))
    {
        while(!file.atEnd())
        {
            this->planText=file.readLine();
            this->planTime=file.readLine();
        }
        ui->label->setText(this->planText);
        QTime time;
        QDateTime endDateTime(QDate::currentDate(),time.fromString(this->planTime,"hh:mm:ss"));
//        QDateTime endDateTime(QDate::currentDate(),time.currentTime().addSecs(1800));
        int endTime=endDateTime.toTime_t();
        //获取当前时间的时间戳
        int nowTime=QDateTime::currentDateTime().toTime_t();
        //计算剩余秒数
        int ddl= endTime-nowTime;
        int h=ddl / 3600;
        int m=ddl % 3600 / 60;
        int s=ddl % 60;
        ui->timeEdit->setTime(QTime(h,m,s));


        t5->start(1000);
        connect(t5,&QTimer::timeout,[this,endTime](){
            int nowTime=QDateTime::currentDateTime().toTime_t();
            int ddl= endTime-nowTime;
            int h=ddl / 3600;
            int m=ddl % 3600 / 60;
            int s=ddl % 60;
            ui->timeEdit->setTime(QTime(h,m,s));
        });
    }
    else
    {
        ui->label_3->setText("请设置计划和时间,时间格式为22:30:00");
    }
}

void Widget::on_pushButton_5_clicked()
{
    AddForm *line=new AddForm();
    line->show();
    connect(line,SIGNAL(plantext(QString)),this,SLOT(resivtext(QString)));
    connect(line,SIGNAL(plantime(QString)),this,SLOT(resivtime(QString)));
}

void Widget::resivtext(QString text)
{
    t5->stop();
    this->planText="";
    this->planText=text;
    t5->stop();
    this->reflushAdd();
}

void Widget::resivtime(QString time)
{
    t5->stop();
    this->planTime="";
    this->planTime=time;
    qDebug()<<time;

    this->reflushAdd();
}

void Widget::reflushAdd()
{
    t5->stop();
    ui->label->setText(this->planText);
    QTime time;
    QDateTime endDateTime(QDate::currentDate(),time.fromString(this->planTime,"hh:mm:ss"));
    int endTime=endDateTime.toTime_t();
    //获取当前时间的时间戳
    int nowTime=QDateTime::currentDateTime().toTime_t();
    //计算剩余秒数
    int ddl= endTime-nowTime;
    int h=ddl / 3600;
    int m=ddl % 3600 / 60;
    int s=ddl % 60;
    ui->timeEdit->setTime(QTime(h,m,s));

    t5=new QTimer(this);
    t5->start(1000);
    connect(t5,&QTimer::timeout,[this,endTime](){
        int nowTime=QDateTime::currentDateTime().toTime_t();
        int ddl= endTime-nowTime;
        int h=ddl / 3600;
        int m=ddl % 3600 / 60;
        int s=ddl % 60;
        ui->timeEdit->setTime(QTime(h,m,s));
    });
}

void Widget::on_Widget_customContextMenuRequested(const QPoint &pos)
{
    this->winMenu->exec(QCursor::pos());
}
